package com.caozhen.manage.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.DigestUtils;

public class SysUtils {

	/**
	 * md5加密
	 * @param target加密字段
	 * @param salt混淆盐值
	 * @return
	 */
	public static String getMd5(String target, String salt) {
		return DigestUtils.md5DigestAsHex((target + "/" + salt).getBytes());
	}

	/**
	 * 发送post请求
	 * @param url路径
	 * @param formparams参数
	 */
	public static String sendPost(String url, List<NameValuePair> formParams) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		UrlEncodedFormEntity entity;
		if (formParams == null) {
			formParams = new ArrayList<NameValuePair>();
		}
		try {
			entity = new UrlEncodedFormEntity(formParams, "UTF-8");
			httpPost.setEntity(entity);
			CloseableHttpResponse response = httpClient.execute(httpPost);
			try {
				if (response.getStatusLine().getStatusCode() == 200) {
					HttpEntity httpEntity = response.getEntity();
					if (httpEntity != null) {
						return EntityUtils.toString(httpEntity, "UTF-8");
					}
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 发送get请求
	 * @param url路径
	 * @param params参数
	 * @return
	 */
	public static String sendGet(String url, Map<String, String> params) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String paramStr = "";
		for (Map.Entry<String, String> entry : params.entrySet()) {
			paramStr += paramStr = "&" + entry.getKey() + "=" + entry.getValue();
		}
		if (!"".equals(paramStr)) {
			paramStr = paramStr.replaceFirst("&", "?");
			url += paramStr;
		}
		try {
			HttpGet httpget = new HttpGet(url);
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				if (response.getStatusLine().getStatusCode() == 200) {
					HttpEntity entity = response.getEntity();
					if (entity != null) {
						return EntityUtils.toString(entity, "UTF-8");
					}
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 生成14位订单号
	 * @param mechineId 
	 * @return
	 */
	public static String createOrderNumber(String mechineId) {
		Integer uuid = UUID.randomUUID().toString().hashCode();
		if (uuid < 0) {
			uuid = -uuid;
		}
		return mechineId + String.format("%010d", uuid) + 
				String.format("%03d", new Random().nextInt(999));
	}

}
